package com.senken.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.senken.base.BaseController;
import com.senken.commons.result.Result;
import com.senken.commons.shiro.ShiroUser;
import com.senken.entity.SkUser;
import com.senken.service.ISkUserService;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController{

	@Autowired
	private ISkUserService userservice;
	
	@RequestMapping("/selectuser")
	@ResponseBody
	public SkUser selectuser(){
		ShiroUser user1 = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		SkUser user = userservice.selectByUsername(user1.loginName);
		SkUser user2 = new SkUser();
		user2.setEmail(user.getEmail());
		user2.setLogintime(user.getLogintime());
		user2.setLogout(user.getLogout());
		user2.setPhone(user.getPhone());
		return user2;
		
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Object addUser(String username,String email,String password,String nickname,String phone,String pwd,int role){
		SkUser user = userservice.selectByUsername(username);
		if(user!=null){
			return renderError("账号已存在");
		}
		if(!(password.equals(pwd))){
			return renderError("两次密码不一致");
		}
		if(userservice.addUser(username, email, password, nickname, phone,role)){
			return renderSuccess();
		}
		return renderError("添加失败");
	}
	
	@RequestMapping("/updatepwd")
	@ResponseBody
	public Object updatepwd(String username,String oldpassword,String newpassword,String pwd ){
		return userservice.updatePwd(username, newpassword, oldpassword, pwd);
	}
	
	@RequestMapping("/findUserbyrole")
	@ResponseBody
	public List<SkUser> findUserbyrole(int roleid){
		return userservice.findUserByRole(roleid);
	}
	
	@RequestMapping("/counts")
	@ResponseBody
	public int counts(){
		return userservice.counts();
	}
	
	@RequestMapping("/findAll")
	@ResponseBody
	public List<SkUser> findAll(int page){
		return userservice.findAll(page);
	}
}
